/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */
#include "DiagramView.h"
#include "DiagramNode.h"
#include "DiagramEdge.h"

namespace QSThink
{
	
	DiagramView::DiagramView()
	{
		_scene = new DiagramScene(this);

		_scene->setItemIndexMethod(QGraphicsScene::NoIndex);
		setScene(_scene);

		setCacheMode(CacheBackground);
		setViewportUpdateMode(BoundingRectViewportUpdate);
		setRenderHint(QPainter::Antialiasing);
		setTransformationAnchor(AnchorUnderMouse);
		setResizeAnchor(AnchorViewCenter);

		
		setCacheMode(CacheBackground);
		setViewportUpdateMode(BoundingRectViewportUpdate);
		setRenderHint(QPainter::Antialiasing);
		setTransformationAnchor(AnchorUnderMouse);
		setResizeAnchor(AnchorViewCenter);
		setMouseTracking(true);


		

		//scale(qreal(0.8), qreal(0.8));
		setMinimumSize(808, 458);
		setWindowTitle(tr("Diagram"));

		//setDragMode(ScrollHandDrag);
		

	}

	DiagramView::~DiagramView()
	{

	}

	void DiagramView::load( QString symbol )
	{
		_scene->loadDayPrice(symbol);
	}

	void DiagramView::loadCapital( QString symbol )
	{
		_scene->loadDayCapital(symbol);
	}

	

	void DiagramView::load( QString symbol,QDate date )
	{
		_scene->loadDayPrice(symbol,date);


	}

	void DiagramView::itemMoved()
	{
		if (!_timerId){
			_timerId = startTimer(1000 / 25);
		}


	}

	void DiagramView::keyPressEvent( QKeyEvent *event )
	{

		QGraphicsView::keyPressEvent(event);

	}

	void DiagramView::timerEvent( QTimerEvent *event )
	{
		return;
	}

	void DiagramView::wheelEvent( QWheelEvent *evt )
	{

		scaleView(pow((double)2, -evt->delta() / 240.0));

	}

	void DiagramView::drawBackground( QPainter *painter, const QRectF &rect )
	{
		
		QRectF rec=_scene->sceneRect();
		
		painter->fillRect(rec, Qt::lightGray);

		
		
	}

	void DiagramView::scaleView( qreal scaleFactor )
	{
		return;
		qreal factor = matrix().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();
		if (factor < 1 || factor > 10){
			return;
		}

		scale(scaleFactor, scaleFactor);

	}

	void DiagramView::mouseMoveEvent( QMouseEvent * event )
	{
		int x=event->x();
		int y=event->y();

		Transaction *trans=_scene->getNearestTrans(x);


		int cnt=_listeners.size();
		for(int i=0;i<cnt;i++){
			_listeners[i]->OnTransactionChanged(trans);
		}

		_scene->OnMouseMove(x,y);
		/*
		Qt::MouseButtons buttons=event->buttons();

		if(buttons&Qt::MidButton){
			dragView(event->globalPos());
			_midGlobalPos=event->globalPos();

		}

		*/
		//QGraphicsView::mouseMoveEvent(event);

	}

	void DiagramView::dragView( QPoint pt )
	{
		QPoint offset=pt-_midGlobalPos;
		

		this->translate(offset.x(),offset.y());


		/*
		

		this->translate(offset.x(),offset.y());
		*/
	}

	void DiagramView::mousePressEvent( QMouseEvent * event )
	{

		Qt::MouseButton button=event->button();

		switch(button){
			case Qt::MidButton:
				_midGlobalPos=event->globalPos();
				break;
		}

		QGraphicsView::mousePressEvent(event);


	}

	void DiagramView::OnEnterNode( DiagramNode* node )
	{
		
		_scene->showInfo(node);

	}

	void DiagramView::OnLeaveNode( DiagramNode* node )
	{
		_scene->hideInfo();
	}

	void DiagramView::addListener( DiagramViewListener* l )
	{
		_listeners.push_back(l);

	}

	void DiagramView::removeListener( DiagramViewListener* l )
	{

		int cnt=_listeners.size();
		std::vector<DiagramViewListener*>::iterator it=_listeners.begin();

		for(int i=0;i<cnt;i++){
			if(_listeners[i]==l){
				_listeners.erase(it);
				break;
			}
			it++;
		}


	}

	int DiagramView::getHighestPriceYPos()
	{
		return _scene->getHighestPriceYPos();
	}

	int DiagramView::getLowestPriceYPos()
	{
		return _scene->getLowestPriceYPos();
	}

	void DiagramView::loadPriceEx( QString symbol )
	{

		_scene->loadDayPriceEx(symbol);
	}


	DiagramViewListener::DiagramViewListener(DiagramView* view)
	{
		_view=view;
		view->addListener(this);
	}

	DiagramViewListener::~DiagramViewListener()
	{
		_view->removeListener(this);

	}

}
